﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using com.subb.WebFramework;
using WebEntities;
using WebEntities.Models.Framework;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class MemberMenuController : Controller
    {
        public ActionResult Index(string gid)
        {
            ViewBag.GID = gid;
            return View();
        }

        public ActionResult GetAllMemberMenu(string gid, StoreRequestParameters parms)
        {
            FW_MenuDAL mdal = new FW_MenuDAL();
            int ttlCount;
            List<FW_MemberMenu> memMenuList = mdal.GetAllMemberMenu(gid, parms.Start, parms.Limit, out ttlCount);
            List<UIMenu> menulist = new List<UIMenu>();
            foreach (var memMenu in memMenuList)
            {
                UIMenu uimenu = UIMenu.AsUIMenu(memMenu.FW_Menu);
                menulist.Add(uimenu);
            }
            return this.Store(menulist, ttlCount);
        }

        [HttpPost]
        public ActionResult DeleteMemberMenu(string gid, List<UIMenu> delMenu)
        {
            try
            {
                FW_MenuDAL mdal = new FW_MenuDAL();
                foreach (var menu in delMenu)
                {
                    mdal.RemoveMemberMenu(gid, menu.MenuID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewMemMenuWindow(string gid)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建用户菜单",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.Add,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateMemMenu"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("gid", gid));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateMemMenu(string gid)
        {
            ViewBag.GID = gid;
            return View();
        }

        [HttpPost]
        public ActionResult CreateMemMenu(string gid, int menuId, int? sortNum)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                FW_MenuDAL mdal = new FW_MenuDAL();
                mdal.CreateMemberMenu(gid, menuId, sortNum ??0);
                //添加菜单栏成功后，把相关菜单项也添加进来
                FW_MenuItemDAL midal = new FW_MenuItemDAL();
                List<FW_MenuItem> itemlist = midal.GetMenuItemsByMenuID(menuId);
                foreach (var mitem in itemlist)
                {
                    FW_MemberMuItem memMuItem = midal.GetMemberMuItem(gid, mitem.ItemID);
                    if (memMuItem == null)
                    {
                        midal.CreateMemberMuItem(gid, mitem.ItemID, 0);
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "用户菜单创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
